<?
session_start();
require_once "funciones.php";
$sucursal=$_SESSION['caweb']['sucursal'];

switch ($_REQUEST['op'])
{
	case "RESFAC":
			echo "<center>Resumen de Facturacion<br><br>";
			echo "<table border=1>";
			$sucursales="<tr bgcolor='aabbcc'><td>Facturacion Neta del Mes ";
			$facturadoantes="<tr><td>Fact. Octubre 2009 (Antes de Iva): ";
			$facturadoantes2="<tr><td>Fact. Noviembre 2009 (Antes de Iva): ";
			$facturadoantes3="<tr><td>Fact. Diciembre 2009 (Antes de Iva): ";
			$facturadoantes4="<tr><td>Fact. Enero 2010 (Antes de Iva): ";
			$facturadoantes5="<tr><td>Fact. Febrero 2010 (Antes de Iva): ";
			$facturadoantes6="<tr><td>Fact. Marzo 2010 (Antes de Iva): ";
            $facturadoantes7="<tr><td>Fact. Abril 2010 (Antes de Iva): ";
            $facturadoantes8="<tr><td>Fact. Mayo 2010 (Antes de Iva): ";
            $facturadoantes9="<tr><td>Fact. Junio 2010 (Antes de Iva): ";
            $facturadoantes10="<tr><td>Fact. Julio 2010 (Antes de Iva): ";
            $facturadoantes11="<tr><td>Fact. Agosto 2010 (Antes de Iva): ";
            $facturadoantes12="<tr><td>Fact. Septiembre 2010 (Antes de Iva): ";
            $facturadoantes13="<tr><td>Fact. Octubre 2010 (Antes de Iva): ";            
            $facturadoantes14="<tr><td>Fact. Noviembre 2010 (Antes de Iva): ";
            $facturadoantes15="<tr><td>Fact. Diciembre 2010 (Antes de Iva): ";
            $facturadoantes16="<tr><td>Fact. Enero 2011 (Antes de Iva): ";
            $facturadoantes17="<tr><td>Fact. Febrero 2011 (Antes de Iva): ";
            $facturadoantes18="<tr><td>Fact. Marzo 2011 (Antes de Iva): ";            
            $facturadoantes19="<tr><td>Fact. Abril 2011 (Antes de Iva): ";
			$facturado="<tr><td>Fact. ".date('F Y')." (Antes de Iva): ";
			$potencial="<tr><td>Potencial a HOY (Base de Facturacion): ";
			$link_caweb = con_caweb();
	
	
			// mes anterior octubre
			$total=0;
			$totalanulado=0;
			$result ="SELECT * FROM sucursales";
			$result=mysql_query($result,$link_caweb) or die('Error sucursales, '.mysql_error());
			while ($row = mysql_fetch_array($result))
			{
				$result2 ="SELECT SUM(total-iva) AS facturado FROM cabeceras_fac WHERE factura > 0 AND sucursal = '$row[codigosucursal]' 
						AND ((estado = 'V' AND fechafactura BETWEEN '2009-10-01' AND '2009-10-31')
						OR (estado = 'A' AND fechafactura BETWEEN '2009-10-01' AND '2009-10-31' AND fechaanulo > '2009-10-31')) 
					 	GROUP BY sucursal";
				$result2=mysql_query($result2,$link_caweb) or die('Error cabeceras_fac, '.mysql_error());
				$row2 = mysql_fetch_array($result2);
		
				$facturadoantes.="<td>".number_format($row2[facturado],0, ',', '.')."</td>";

				$total=$total+$row2[facturado];

				$result2 ="SELECT SUM(total-iva) AS facturado FROM cabeceras_fac WHERE factura > 0 AND sucursal = '$row[codigosucursal]'
						AND (estado = 'A' AND fechafactura < '2009-10-01' AND DATE(fechaanulo) BETWEEN '2009-10-01' AND '2009-10-31') 
					 	GROUP BY sucursal";
				$result2=mysql_query($result2,$link_caweb) or die('Error cabeceras_fac, '.mysql_error());
				$row2 = mysql_fetch_array($result2);
		
				$facturadoantes.="<td>".number_format($row2[facturado],0, ',', '.')."</td>";
				$totalanulado=$totalanulado+$row2[facturado];
			}
			
			$facturadoantes.="<td>".number_format($total,0, ',', '.')."</td>";
			$facturadoantes.="<td>".number_format($totalanulado,0, ',', '.')."</td></tr>";
	
		// mes anterior noviembre
			$total=0;
			$totalanulado=0;
			$result ="SELECT * FROM sucursales";
			$result=mysql_query($result,$link_caweb) or die('Error sucursales, '.mysql_error());
			while ($row = mysql_fetch_array($result))
			{
				$result2 ="SELECT SUM(total-iva) AS facturado FROM cabeceras_fac WHERE factura > 0 AND sucursal = '$row[codigosucursal]' 
						AND ((estado = 'V' AND fechafactura BETWEEN '2009-11-01' AND '2009-11-30')
						OR (estado = 'A' AND fechafactura BETWEEN '2009-11-01' AND '2009-11-30' AND fechaanulo > '2009-11-30')) 
					 	GROUP BY sucursal";
				$result2=mysql_query($result2,$link_caweb) or die('Error cabeceras_fac, '.mysql_error());
				$row2 = mysql_fetch_array($result2);
		
				$facturadoantes2.="<td>".number_format($row2[facturado],0, ',', '.')."</td>";

				$total=$total+$row2[facturado];

				$result2 ="SELECT SUM(total-iva) AS facturado FROM cabeceras_fac WHERE factura > 0 AND sucursal = '$row[codigosucursal]'
						AND (estado = 'A' AND fechafactura < '2009-11-01' AND DATE(fechaanulo) BETWEEN '2009-11-01' AND '2009-11-30') 
					 	GROUP BY sucursal";
				$result2=mysql_query($result2,$link_caweb) or die('Error cabeceras_fac, '.mysql_error());
				$row2 = mysql_fetch_array($result2);
		
				$facturadoantes2.="<td>".number_format($row2[facturado],0, ',', '.')."</td>";
				$totalanulado=$totalanulado+$row2[facturado];
			}
			
			$facturadoantes2.="<td>".number_format($total,0, ',', '.')."</td>";
			$facturadoantes2.="<td>".number_format($totalanulado,0, ',', '.')."</td></tr>";	


			// mes anterior diciembre
			$total=0;
			$totalanulado=0;
			$result ="SELECT * FROM sucursales";
			$result=mysql_query($result,$link_caweb) or die('Error sucursales, '.mysql_error());
			while ($row = mysql_fetch_array($result))
			{
				$result2 ="SELECT SUM(total-iva) AS facturado FROM cabeceras_fac WHERE factura > 0 AND sucursal = '$row[codigosucursal]' 
						AND ((estado = 'V' AND fechafactura BETWEEN '2009-12-01' AND '2009-12-31')
						OR (estado = 'A' AND fechafactura BETWEEN '2009-12-01' AND '2009-12-31' AND fechaanulo > '2009-12-31')) 
					 	GROUP BY sucursal";
				$result2=mysql_query($result2,$link_caweb) or die('Error cabeceras_fac, '.mysql_error());
				$row2 = mysql_fetch_array($result2);
		
				$facturadoantes3.="<td>".number_format($row2[facturado],0, ',', '.')."</td>";

				$total=$total+$row2[facturado];

				$result2 ="SELECT SUM(total-iva) AS facturado FROM cabeceras_fac WHERE factura > 0 AND sucursal = '$row[codigosucursal]'
						AND (estado = 'A' AND fechafactura < '2009-12-01' AND DATE(fechaanulo) BETWEEN '2009-12-01' AND '2009-12-31') 
					 	GROUP BY sucursal";
				$result2=mysql_query($result2,$link_caweb) or die('Error cabeceras_fac, '.mysql_error());
				$row2 = mysql_fetch_array($result2);
		
				$facturadoantes3.="<td>".number_format($row2[facturado],0, ',', '.')."</td>";
			
				$totalanulado=$totalanulado+$row2[facturado];
			}
			
			$facturadoantes3.="<td>".number_format($total,0, ',', '.')."</td>";
			$facturadoantes3.="<td>".number_format($totalanulado,0, ',', '.')."</tr>";


			// mes anterior Enero
			$total=0;
			$totalanulado=0;
			$result ="SELECT * FROM sucursales";
			$result=mysql_query($result,$link_caweb) or die('Error sucursales, '.mysql_error());
			while ($row = mysql_fetch_array($result))
			{
				$result2 ="SELECT SUM(total-iva) AS facturado FROM cabeceras_fac WHERE factura > 0 AND sucursal = '$row[codigosucursal]' 
						AND ((estado = 'V' AND fechafactura BETWEEN '2010-01-01' AND '2010-01-31')
						OR (estado = 'A' AND fechafactura BETWEEN '2010-01-01' AND '2010-01-31' AND fechaanulo > '2010-01-31')) 
					 	GROUP BY sucursal";
				$result2=mysql_query($result2,$link_caweb) or die('Error cabeceras_fac, '.mysql_error());
				$row2 = mysql_fetch_array($result2);
		
				$facturadoantes4.="<td>".number_format($row2[facturado],0, ',', '.')."</td>";

				$total=$total+$row2[facturado];

				$result2 ="SELECT SUM(total-iva) AS facturado FROM cabeceras_fac WHERE factura > 0 AND sucursal = '$row[codigosucursal]'
						AND (estado = 'A' AND fechafactura < '2010-01-01' AND DATE(fechaanulo) BETWEEN '2010-01-01' AND '2010-01-31') 
					 	GROUP BY sucursal";
				$result2=mysql_query($result2,$link_caweb) or die('Error cabeceras_fac, '.mysql_error());
				$row2 = mysql_fetch_array($result2);
		
				$facturadoantes4.="<td>".number_format($row2[facturado],0, ',', '.')."</td>";
			
				$totalanulado=$totalanulado+$row2[facturado];
			}
			
			$facturadoantes4.="<td>".number_format($total,0, ',', '.')."</td>";
			$facturadoantes4.="<td>".number_format($totalanulado,0, ',', '.')."</tr>";
			
			
			// mes anterior Febrero
			$total=0;
			$totalanulado=0;
			$result ="SELECT * FROM sucursales";
			$result=mysql_query($result,$link_caweb) or die('Error sucursales, '.mysql_error());
			while ($row = mysql_fetch_array($result))
			{
				$result2 ="SELECT SUM(total-iva) AS facturado FROM cabeceras_fac WHERE factura > 0 AND sucursal = '$row[codigosucursal]' 
						AND ((estado = 'V' AND fechafactura BETWEEN '2010-02-01' AND '2010-02-28')
						OR (estado = 'A' AND fechafactura BETWEEN '2010-02-01' AND '2010-02-28' AND fechaanulo > '2010-02-28')) 
					 	GROUP BY sucursal";
				$result2=mysql_query($result2,$link_caweb) or die('Error cabeceras_fac, '.mysql_error());
				$row2 = mysql_fetch_array($result2);
		
				$facturadoantes5.="<td>".number_format($row2[facturado],0, ',', '.')."</td>";

				$total=$total+$row2[facturado];

				$result2 ="SELECT SUM(total-iva) AS facturado FROM cabeceras_fac WHERE factura > 0 AND sucursal = '$row[codigosucursal]' 
						AND (estado = 'A' AND fechafactura < '2010-02-01' AND DATE(fechaanulo) BETWEEN '2010-02-01' AND '2010-02-28') 
					 	GROUP BY sucursal";
				$result2=mysql_query($result2,$link_caweb) or die('Error cabeceras_fac, '.mysql_error());
				$row2 = mysql_fetch_array($result2);
		
				$facturadoantes5.="<td>".number_format($row2[facturado],0, ',', '.')."</td>";
			
				$totalanulado=$totalanulado+$row2[facturado];
			}
			
			$facturadoantes5.="<td>".number_format($total,0, ',', '.')."</td></td>";
			$facturadoantes5.="<td>".number_format($totalanulado,0, ',', '.')."</tr>";

            // mes anterior Marzo
			$total=0;
			$totalanulado=0;
			$result ="SELECT * FROM sucursales";
			$result=mysql_query($result,$link_caweb) or die('Error sucursales, '.mysql_error());
			while ($row = mysql_fetch_array($result))
			{
				$result2 ="SELECT SUM(total-iva) AS facturado FROM cabeceras_fac WHERE factura > 0 AND sucursal = '$row[codigosucursal]' 
						AND ((estado = 'V' AND fechafactura BETWEEN '2010-03-01' AND '2010-03-31')
						OR (estado = 'A' AND fechafactura BETWEEN '2010-03-01' AND '2010-03-31' AND fechaanulo > '2010-03-31')) 
					 	GROUP BY sucursal";
				$result2=mysql_query($result2,$link_caweb) or die('Error cabeceras_fac, '.mysql_error());
				$row2 = mysql_fetch_array($result2);
		
				$facturadoantes6.="<td>".number_format($row2[facturado],0, ',', '.')."</td>";

				$total=$total+$row2[facturado];

				$result2 ="SELECT SUM(total-iva) AS facturado FROM cabeceras_fac WHERE factura > 0 AND sucursal = '$row[codigosucursal]' 
						AND (estado = 'A' AND fechafactura < '2010-03-01' AND DATE(fechaanulo) BETWEEN '2010-03-01' AND '2010-03-31') 
					 	GROUP BY sucursal";
				$result2=mysql_query($result2,$link_caweb) or die('Error cabeceras_fac, '.mysql_error());
				$row2 = mysql_fetch_array($result2);
		
				$facturadoantes6.="<td>".number_format($row2[facturado],0, ',', '.')."</td>";
			
				$totalanulado=$totalanulado+$row2[facturado];
			}
			
			$facturadoantes6.="<td>".number_format($total,0, ',', '.')."</td></td>";
			$facturadoantes6.="<td>".number_format($totalanulado,0, ',', '.')."</tr>";

	
            // mes anterior Abril
			$total=0;
			$totalanulado=0;
			$result ="SELECT * FROM sucursales";
			$result=mysql_query($result,$link_caweb) or die('Error sucursales, '.mysql_error());
			while ($row = mysql_fetch_array($result))
			{
				$result2 ="SELECT SUM(total-iva) AS facturado FROM cabeceras_fac WHERE factura > 0 AND sucursal = '$row[codigosucursal]' 
						AND ((estado = 'V' AND fechafactura BETWEEN '2010-04-01' AND '2010-04-30')
						OR (estado = 'A' AND fechafactura BETWEEN '2010-04-01' AND '2010-04-30' AND fechaanulo > '2010-04-30')) 
					 	GROUP BY sucursal";
				$result2=mysql_query($result2,$link_caweb) or die('Error cabeceras_fac, '.mysql_error());
				$row2 = mysql_fetch_array($result2);
		
				$facturadoantes7.="<td>".number_format($row2[facturado],0, ',', '.')."</td>";

				$total=$total+$row2[facturado];

				$result2 ="SELECT SUM(total-iva) AS facturado FROM cabeceras_fac WHERE factura > 0 AND sucursal = '$row[codigosucursal]' 
						AND (estado = 'A' AND fechafactura < '2010-04-01' AND DATE(fechaanulo) BETWEEN '2010-04-01' AND '2010-04-30') 
					 	GROUP BY sucursal";
				$result2=mysql_query($result2,$link_caweb) or die('Error cabeceras_fac, '.mysql_error());
				$row2 = mysql_fetch_array($result2);
		
				$facturadoantes7.="<td>".number_format($row2[facturado],0, ',', '.')."</td>";
			
				$totalanulado=$totalanulado+$row2[facturado];
			}
			
			$facturadoantes7.="<td>".number_format($total,0, ',', '.')."</td></td>";
			$facturadoantes7.="<td>".number_format($totalanulado,0, ',', '.')."</tr>";


            // mes anterior Mayo
			$total=0;
			$totalanulado=0;
			$result ="SELECT * FROM sucursales";
			$result=mysql_query($result,$link_caweb) or die('Error sucursales, '.mysql_error());
			while ($row = mysql_fetch_array($result))
			{
				$result2 ="SELECT SUM(total-iva) AS facturado FROM cabeceras_fac WHERE factura > 0 AND sucursal = '$row[codigosucursal]' 
						AND ((estado = 'V' AND fechafactura BETWEEN '2010-05-01' AND '2010-05-31')
						OR (estado = 'A' AND fechafactura BETWEEN '2010-05-01' AND '2010-05-31' AND fechaanulo > '2010-05-31')) 
					 	GROUP BY sucursal";
				$result2=mysql_query($result2,$link_caweb) or die('Error cabeceras_fac, '.mysql_error());
				$row2 = mysql_fetch_array($result2);
		
				$facturadoantes8.="<td>".number_format($row2[facturado],0, ',', '.')."</td>";

				$total=$total+$row2[facturado];

				$result2 ="SELECT SUM(total-iva) AS facturado FROM cabeceras_fac WHERE factura > 0 AND sucursal = '$row[codigosucursal]' 
						AND (estado = 'A' AND fechafactura < '2010-05-01' AND DATE(fechaanulo) BETWEEN '2010-05-01' AND '2010-05-31') 
					 	GROUP BY sucursal";
				$result2=mysql_query($result2,$link_caweb) or die('Error cabeceras_fac, '.mysql_error());
				$row2 = mysql_fetch_array($result2);
		
				$facturadoantes8.="<td>".number_format($row2[facturado],0, ',', '.')."</td>";
			
				$totalanulado=$totalanulado+$row2[facturado];
			}
			
			$facturadoantes8.="<td>".number_format($total,0, ',', '.')."</td></td>";
			$facturadoantes8.="<td>".number_format($totalanulado,0, ',', '.')."</tr>";


            // mes anterior junio
			$total=0;
			$totalanulado=0;
			$result ="SELECT * FROM sucursales";
			$result=mysql_query($result,$link_caweb) or die('Error sucursales, '.mysql_error());
			while ($row = mysql_fetch_array($result))
			{
				$result2 ="SELECT SUM(total-iva) AS facturado FROM cabeceras_fac WHERE factura > 0 AND sucursal = '$row[codigosucursal]' 
						AND ((estado = 'V' AND fechafactura BETWEEN '2010-06-01' AND '2010-06-30')
						OR (estado = 'A' AND fechafactura BETWEEN '2010-06-01' AND '2010-06-30' AND fechaanulo > '2010-06-30')) 
					 	GROUP BY sucursal";
				$result2=mysql_query($result2,$link_caweb) or die('Error cabeceras_fac, '.mysql_error());
				$row2 = mysql_fetch_array($result2);
		
				$facturadoantes9.="<td>".number_format($row2[facturado],0, ',', '.')."</td>";

				$total=$total+$row2[facturado];

				$result2 ="SELECT SUM(total-iva) AS facturado FROM cabeceras_fac WHERE factura > 0 AND sucursal = '$row[codigosucursal]' 
						AND (estado = 'A' AND fechafactura < '2010-06-01' AND DATE(fechaanulo) BETWEEN '2010-06-01' AND '2010-06-30') 
					 	GROUP BY sucursal";
				$result2=mysql_query($result2,$link_caweb) or die('Error cabeceras_fac, '.mysql_error());
				$row2 = mysql_fetch_array($result2);
		
				$facturadoantes9.="<td>".number_format($row2[facturado],0, ',', '.')."</td>";
			
				$totalanulado=$totalanulado+$row2[facturado];
			}
			
			$facturadoantes9.="<td>".number_format($total,0, ',', '.')."</td></td>";
			$facturadoantes9.="<td>".number_format($totalanulado,0, ',', '.')."</tr>";


            // mes anterior julio
			$total=0;
			$totalanulado=0;
			$result ="SELECT * FROM sucursales";
			$result=mysql_query($result,$link_caweb) or die('Error sucursales, '.mysql_error());
			while ($row = mysql_fetch_array($result))
			{
				$result2 ="SELECT SUM(total-iva) AS facturado FROM cabeceras_fac WHERE factura > 0 AND sucursal = '$row[codigosucursal]' 
						AND ((estado = 'V' AND fechafactura BETWEEN '2010-07-01' AND '2010-07-31')
						OR (estado = 'A' AND fechafactura BETWEEN '2010-07-01' AND '2010-07-31' AND fechaanulo > '2010-07-31')) 
					 	GROUP BY sucursal";
				$result2=mysql_query($result2,$link_caweb) or die('Error cabeceras_fac, '.mysql_error());
				$row2 = mysql_fetch_array($result2);
		
				$facturadoantes10.="<td>".number_format($row2[facturado],0, ',', '.')."</td>";

				$total=$total+$row2[facturado];

				$result2 ="SELECT SUM(total-iva) AS facturado FROM cabeceras_fac WHERE factura > 0 AND sucursal = '$row[codigosucursal]' 
						AND (estado = 'A' AND fechafactura < '2010-07-01' AND DATE(fechaanulo) BETWEEN '2010-07-01' AND '2010-07-31') 
					 	GROUP BY sucursal";
				$result2=mysql_query($result2,$link_caweb) or die('Error cabeceras_fac, '.mysql_error());
				$row2 = mysql_fetch_array($result2);
		
				$facturadoantes10.="<td>".number_format($row2[facturado],0, ',', '.')."</td>";
			
				$totalanulado=$totalanulado+$row2[facturado];
			}
			
			$facturadoantes10.="<td>".number_format($total,0, ',', '.')."</td></td>";
			$facturadoantes10.="<td>".number_format($totalanulado,0, ',', '.')."</tr>";


            // mes anterior Agosto
			$total=0;
			$totalanulado=0;
			$result ="SELECT * FROM sucursales";
			$result=mysql_query($result,$link_caweb) or die('Error sucursales, '.mysql_error());
			while ($row = mysql_fetch_array($result))
			{
				$result2 ="SELECT SUM(total-iva) AS facturado FROM cabeceras_fac WHERE factura > 0 AND sucursal = '$row[codigosucursal]' 
						AND ((estado = 'V' AND fechafactura BETWEEN '2010-08-01' AND '2010-08-31')
						OR (estado = 'A' AND fechafactura BETWEEN '2010-08-01' AND '2010-08-31' AND fechaanulo > '2010-08-31')) 
					 	GROUP BY sucursal";
				$result2=mysql_query($result2,$link_caweb) or die('Error cabeceras_fac, '.mysql_error());
				$row2 = mysql_fetch_array($result2);
		
				$facturadoantes11.="<td>".number_format($row2[facturado],0, ',', '.')."</td>";

				$total=$total+$row2[facturado];

				$result2 ="SELECT SUM(total-iva) AS facturado FROM cabeceras_fac WHERE factura > 0 AND sucursal = '$row[codigosucursal]' 
						AND (estado = 'A' AND fechafactura < '2010-08-01' AND DATE(fechaanulo) BETWEEN '2010-08-01' AND '2010-08-31') 
					 	GROUP BY sucursal";
				$result2=mysql_query($result2,$link_caweb) or die('Error cabeceras_fac, '.mysql_error());
				$row2 = mysql_fetch_array($result2);
		
				$facturadoantes11.="<td>".number_format($row2[facturado],0, ',', '.')."</td>";
			
				$totalanulado=$totalanulado+$row2[facturado];
			}
			
			$facturadoantes11.="<td>".number_format($total,0, ',', '.')."</td></td>";
			$facturadoantes11.="<td>".number_format($totalanulado,0, ',', '.')."</tr>";

            // mes anterior septiembre
			$total=0;
			$totalanulado=0;
			$result ="SELECT * FROM sucursales";
			$result=mysql_query($result,$link_caweb) or die('Error sucursales, '.mysql_error());
			while ($row = mysql_fetch_array($result))
			{
				$result2 ="SELECT SUM(total-iva) AS facturado FROM cabeceras_fac WHERE factura > 0 AND sucursal = '$row[codigosucursal]' 
						AND ((estado = 'V' AND fechafactura BETWEEN '2010-09-01' AND '2010-09-30')
						OR (estado = 'A' AND fechafactura BETWEEN '2010-09-01' AND '2010-09-30' AND fechaanulo > '2010-09-30')) 
					 	GROUP BY sucursal";
				$result2=mysql_query($result2,$link_caweb) or die('Error cabeceras_fac, '.mysql_error());
				$row2 = mysql_fetch_array($result2);
		
				$facturadoantes12.="<td>".number_format($row2[facturado],0, ',', '.')."</td>";

				$total=$total+$row2[facturado];

				$result2 ="SELECT SUM(total-iva) AS facturado FROM cabeceras_fac WHERE factura > 0 AND sucursal = '$row[codigosucursal]' 
						AND (estado = 'A' AND fechafactura < '2010-09-01' AND DATE(fechaanulo) BETWEEN '2010-09-01' AND '2010-09-30') 
					 	GROUP BY sucursal";
				$result2=mysql_query($result2,$link_caweb) or die('Error cabeceras_fac, '.mysql_error());
				$row2 = mysql_fetch_array($result2);
		
				$facturadoantes12.="<td>".number_format($row2[facturado],0, ',', '.')."</td>";
			
				$totalanulado=$totalanulado+$row2[facturado];
			}
			
			$facturadoantes12.="<td>".number_format($total,0, ',', '.')."</td></td>";
			$facturadoantes12.="<td>".number_format($totalanulado,0, ',', '.')."</tr>";

            // mes anterior Octubre
			$total=0;
			$totalanulado=0;
			$result ="SELECT * FROM sucursales";
			$result=mysql_query($result,$link_caweb) or die('Error sucursales, '.mysql_error());
			while ($row = mysql_fetch_array($result))
			{
				$result2 ="SELECT SUM(total-iva) AS facturado FROM cabeceras_fac WHERE factura > 0 AND sucursal = '$row[codigosucursal]' 
						AND ((estado = 'V' AND fechafactura BETWEEN '2010-10-01' AND '2010-10-31')
						OR (estado = 'A' AND fechafactura BETWEEN '2010-10-01' AND '2010-10-31' AND fechaanulo > '2010-10-31')) 
					 	GROUP BY sucursal";
				$result2=mysql_query($result2,$link_caweb) or die('Error cabeceras_fac, '.mysql_error());
				$row2 = mysql_fetch_array($result2);
		
				$facturadoantes13.="<td>".number_format($row2[facturado],0, ',', '.')."</td>";

				$total=$total+$row2[facturado];

				$result2 ="SELECT SUM(total-iva) AS facturado FROM cabeceras_fac WHERE factura > 0 AND sucursal = '$row[codigosucursal]' 
						AND (estado = 'A' AND fechafactura < '2010-10-01' AND DATE(fechaanulo) BETWEEN '2010-10-01' AND '2010-10-31') 
					 	GROUP BY sucursal";
				$result2=mysql_query($result2,$link_caweb) or die('Error cabeceras_fac, '.mysql_error());
				$row2 = mysql_fetch_array($result2);
		
				$facturadoantes13.="<td>".number_format($row2[facturado],0, ',', '.')."</td>";
			
				$totalanulado=$totalanulado+$row2[facturado];
			}
			
			$facturadoantes13.="<td>".number_format($total,0, ',', '.')."</td></td>";
			$facturadoantes13.="<td>".number_format($totalanulado,0, ',', '.')."</tr>";


            // mes anterior Noviembre
			$total=0;
			$totalanulado=0;
			$result ="SELECT * FROM sucursales";
			$result=mysql_query($result,$link_caweb) or die('Error sucursales, '.mysql_error());
			while ($row = mysql_fetch_array($result))
			{
				$result2 ="SELECT SUM(total-iva) AS facturado FROM cabeceras_fac WHERE factura > 0 AND sucursal = '$row[codigosucursal]' 
						AND ((estado = 'V' AND fechafactura BETWEEN '2010-11-01' AND '2010-11-30')
						OR (estado = 'A' AND fechafactura BETWEEN '2010-11-01' AND '2010-11-30' AND fechaanulo > '2010-11-30')) 
					 	GROUP BY sucursal";
				$result2=mysql_query($result2,$link_caweb) or die('Error cabeceras_fac, '.mysql_error());
				$row2 = mysql_fetch_array($result2);
		
				$facturadoantes14.="<td>".number_format($row2[facturado],0, ',', '.')."</td>";

				$total=$total+$row2[facturado];

				$result2 ="SELECT SUM(total-iva) AS facturado FROM cabeceras_fac WHERE factura > 0 AND sucursal = '$row[codigosucursal]' 
						AND (estado = 'A' AND fechafactura < '2010-11-01' AND DATE(fechaanulo) BETWEEN '2010-11-01' AND '2010-11-30') 
					 	GROUP BY sucursal";
				$result2=mysql_query($result2,$link_caweb) or die('Error cabeceras_fac, '.mysql_error());
				$row2 = mysql_fetch_array($result2);
		
				$facturadoantes14.="<td>".number_format($row2[facturado],0, ',', '.')."</td>";
			
				$totalanulado=$totalanulado+$row2[facturado];
			}
			
			$facturadoantes14.="<td>".number_format($total,0, ',', '.')."</td></td>";
			$facturadoantes14.="<td>".number_format($totalanulado,0, ',', '.')."</tr>";


            // mes anterior Diciembre
			$total=0;
			$totalanulado=0;
			$result ="SELECT * FROM sucursales";
			$result=mysql_query($result,$link_caweb) or die('Error sucursales, '.mysql_error());
			while ($row = mysql_fetch_array($result))
			{
				$result2 ="SELECT SUM(total-iva) AS facturado FROM cabeceras_fac WHERE factura > 0 AND sucursal = '$row[codigosucursal]' 
						AND ((estado = 'V' AND fechafactura BETWEEN '2010-12-01' AND '2010-12-31')
						OR (estado = 'A' AND fechafactura BETWEEN '2010-12-01' AND '2010-12-31' AND fechaanulo > '2010-12-31')) 
					 	GROUP BY sucursal";
				$result2=mysql_query($result2,$link_caweb) or die('Error cabeceras_fac, '.mysql_error());
				$row2 = mysql_fetch_array($result2);
		
				$facturadoantes15.="<td>".number_format($row2[facturado],0, ',', '.')."</td>";

				$total=$total+$row2[facturado];

				$result2 ="SELECT SUM(total-iva) AS facturado FROM cabeceras_fac WHERE factura > 0 AND sucursal = '$row[codigosucursal]' 
						AND (estado = 'A' AND fechafactura < '2010-12-01' AND DATE(fechaanulo) BETWEEN '2010-12-01' AND '2010-12-31') 
					 	GROUP BY sucursal";
				$result2=mysql_query($result2,$link_caweb) or die('Error cabeceras_fac, '.mysql_error());
				$row2 = mysql_fetch_array($result2);
		
				$facturadoantes15.="<td>".number_format($row2[facturado],0, ',', '.')."</td>";
			
				$totalanulado=$totalanulado+$row2[facturado];
			}
			
			$facturadoantes15.="<td>".number_format($total,0, ',', '.')."</td></td>";
			$facturadoantes15.="<td>".number_format($totalanulado,0, ',', '.')."</tr>";
            
            
            // mes anterior Enero 2011
			$total=0;
			$totalanulado=0;
			$result ="SELECT * FROM sucursales";
			$result=mysql_query($result,$link_caweb) or die('Error sucursales, '.mysql_error());
			while ($row = mysql_fetch_array($result))
			{
				$result2 ="SELECT SUM(total-iva) AS facturado FROM cabeceras_fac WHERE factura > 0 AND sucursal = '$row[codigosucursal]' 
						AND ((estado = 'V' AND fechafactura BETWEEN '2011-01-01' AND '2011-01-31')
						OR (estado = 'A' AND fechafactura BETWEEN '2011-01-01' AND '2011-01-31' AND fechaanulo > '2011-01-31 23:59:59'))
					 	GROUP BY sucursal";
				$result2=mysql_query($result2,$link_caweb) or die('Error cabeceras_fac, '.mysql_error());
				$row2 = mysql_fetch_array($result2);
		
				$facturadoantes16.="<td>".number_format($row2[facturado],0, ',', '.')."</td>";

				$total=$total+$row2[facturado];

				$result2 ="SELECT SUM(total-iva) AS facturado FROM cabeceras_fac WHERE factura > 0 AND sucursal = '$row[codigosucursal]'
						AND (estado = 'A' AND fechafactura < '2011-01-01' AND DATE(fechaanulo) BETWEEN '2011-01-01' AND '2011-01-31') 
					 	GROUP BY sucursal";
				$result2=mysql_query($result2,$link_caweb) or die('Error cabeceras_fac, '.mysql_error());
				$row2 = mysql_fetch_array($result2);
		
				$facturadoantes16.="<td>".number_format($row2[facturado],0, ',', '.')."</td>";
			
				$totalanulado=$totalanulado+$row2[facturado];
			}
			
			$facturadoantes16.="<td>".number_format($total,0, ',', '.')."</td>";
			$facturadoantes16.="<td>".number_format($totalanulado,0, ',', '.')."</tr>";


            // mes anterior Febrero 2011
			$total=0;
			$totalanulado=0;
			$result ="SELECT * FROM sucursales";
			$result=mysql_query($result,$link_caweb) or die('Error sucursales, '.mysql_error());
			while ($row = mysql_fetch_array($result))
			{
				$result2 ="SELECT SUM(total-iva) AS facturado FROM cabeceras_fac WHERE factura > 0 AND sucursal = '$row[codigosucursal]' 
						AND ((estado = 'V' AND fechafactura BETWEEN '2011-02-01' AND '2011-02-28')
						OR (estado = 'A' AND fechafactura BETWEEN '2011-02-01' AND '2011-02-28' AND fechaanulo > '2011-02-28 23:59:59'))
					 	GROUP BY sucursal";
				$result2=mysql_query($result2,$link_caweb) or die('Error cabeceras_fac, '.mysql_error());
				$row2 = mysql_fetch_array($result2);
		
				$facturadoantes17.="<td>".number_format($row2[facturado],0, ',', '.')."</td>";

				$total=$total+$row2[facturado];

				$result2 ="SELECT SUM(total-iva) AS facturado FROM cabeceras_fac WHERE factura > 0 AND sucursal = '$row[codigosucursal]'
						AND (estado = 'A' AND fechafactura < '2011-02-01' AND DATE(fechaanulo) BETWEEN '2011-02-01' AND '2011-02-28') 
					 	GROUP BY sucursal";
				$result2=mysql_query($result2,$link_caweb) or die('Error cabeceras_fac, '.mysql_error());
				$row2 = mysql_fetch_array($result2);
		
				$facturadoantes17.="<td>".number_format($row2[facturado],0, ',', '.')."</td>";
			
				$totalanulado=$totalanulado+$row2[facturado];
			}
			
			$facturadoantes17.="<td>".number_format($total,0, ',', '.')."</td>";
			$facturadoantes17.="<td>".number_format($totalanulado,0, ',', '.')."</tr>";


            // mes anterior Marzo 2011
			$total=0;
			$totalanulado=0;
			$result ="SELECT * FROM sucursales";
			$result=mysql_query($result,$link_caweb) or die('Error sucursales, '.mysql_error());
			while ($row = mysql_fetch_array($result))
			{
				$result2 ="SELECT SUM(total-iva) AS facturado FROM cabeceras_fac WHERE factura > 0 AND sucursal = '$row[codigosucursal]' 
						AND ((estado = 'V' AND fechafactura BETWEEN '2011-03-01' AND '2011-03-31')
						OR (estado = 'A' AND fechafactura BETWEEN '2011-03-01' AND '2011-03-31' AND fechaanulo > '2011-03-31 23:59:59'))
					 	GROUP BY sucursal";
				$result2=mysql_query($result2,$link_caweb) or die('Error cabeceras_fac, '.mysql_error());
				$row2 = mysql_fetch_array($result2);
		
				$facturadoantes18.="<td>".number_format($row2[facturado],0, ',', '.')."</td>";

				$total=$total+$row2[facturado];

				$result2 ="SELECT SUM(total-iva) AS facturado FROM cabeceras_fac WHERE factura > 0 AND sucursal = '$row[codigosucursal]'
						AND (estado = 'A' AND fechafactura < '2011-03-01' AND DATE(fechaanulo) BETWEEN '2011-03-01' AND '2011-03-31') 
					 	GROUP BY sucursal";
				$result2=mysql_query($result2,$link_caweb) or die('Error cabeceras_fac, '.mysql_error());
				$row2 = mysql_fetch_array($result2);
		
				$facturadoantes18.="<td>".number_format($row2[facturado],0, ',', '.')."</td>";
			
				$totalanulado=$totalanulado+$row2[facturado];
			}
			
			$facturadoantes18.="<td>".number_format($total,0, ',', '.')."</td>";
			$facturadoantes18.="<td>".number_format($totalanulado,0, ',', '.')."</tr>";



            // mes anterior Abril 2011
			$total=0;
			$totalanulado=0;
			$result ="SELECT * FROM sucursales";
			$result=mysql_query($result,$link_caweb) or die('Error sucursales, '.mysql_error());
			while ($row = mysql_fetch_array($result))
			{
				$result2 ="SELECT SUM(total-iva) AS facturado FROM cabeceras_fac WHERE factura > 0 AND sucursal = '$row[codigosucursal]' 
						AND ((estado = 'V' AND fechafactura BETWEEN '2011-04-01' AND '2011-04-30')
						OR (estado = 'A' AND fechafactura BETWEEN '2011-04-01' AND '2011-04-30' AND fechaanulo > '2011-04-30 23:59:59'))
					 	GROUP BY sucursal";
				$result2=mysql_query($result2,$link_caweb) or die('Error cabeceras_fac, '.mysql_error());
				$row2 = mysql_fetch_array($result2);
		
				$facturadoantes19.="<td>".number_format($row2[facturado],0, ',', '.')."</td>";

				$total=$total+$row2[facturado];

				$result2 ="SELECT SUM(total-iva) AS facturado FROM cabeceras_fac WHERE factura > 0 AND sucursal = '$row[codigosucursal]'
						AND (estado = 'A' AND fechafactura < '2011-04-01' AND DATE(fechaanulo) BETWEEN '2011-04-01' AND '2011-04-30') 
					 	GROUP BY sucursal";
				$result2=mysql_query($result2,$link_caweb) or die('Error cabeceras_fac, '.mysql_error());
				$row2 = mysql_fetch_array($result2);
		
				$facturadoantes19.="<td>".number_format($row2[facturado],0, ',', '.')."</td>";
			
				$totalanulado=$totalanulado+$row2[facturado];
			}
			
			$facturadoantes19.="<td>".number_format($total,0, ',', '.')."</td>";
			$facturadoantes19.="<td>".number_format($totalanulado,0, ',', '.')."</tr>";



			// mes Mayo 2011
			$total=0;
			$totalanulado=0;
			$result ="SELECT * FROM sucursales";
			$result=mysql_query($result,$link_caweb) or die('Error sucursales, '.mysql_error());
			while ($row = mysql_fetch_array($result))
			{
				$result2 ="SELECT sucursal,SUM(total-iva) AS facturado FROM cabeceras_fac WHERE factura > 0 AND sucursal = '$row[codigosucursal]'
				 		AND ((estado = 'V' AND fechafactura BETWEEN '2011-05-01' AND '2011-05-31')
						OR (estado = 'A' AND fechafactura BETWEEN '2011-05-01' AND '2011-05-31' AND fechaanulo > '2011-05-31')) 
					 	GROUP BY sucursal";
				$result2=mysql_query($result2,$link_caweb) or die('Error cabeceras_fac, '.mysql_error());
				$row2 = mysql_fetch_array($result2);
		
				$sucursales.="<td>".$row[nombre]."<td> Anuladas ";
				$facturado.="<td>".number_format($row2[facturado],0, ',', '.')."</td>";
				$total=$total+$row2[facturado];
				
				$result2 ="SELECT SUM(total-iva) AS facturado FROM cabeceras_fac WHERE factura > 0 AND sucursal = '$row[codigosucursal]' 
						AND (estado = 'A' AND fechafactura < '2011-05-01' AND DATE(fechaanulo) BETWEEN '2011-05-01' AND '2011-05-31') 
					 	GROUP BY sucursal";
				$result2=mysql_query($result2,$link_caweb) or die('Error cabeceras_fac, '.mysql_error());
				$row2 = mysql_fetch_array($result2);
		
				$facturado.="<td>".number_format($row2[facturado],0, ',', '.')."</td>";
				$totalanulado=$totalanulado+$row2[facturado];
			}
			
			$sucursales.="<td> TOTAL <td> Anuladas </tr>";
			$facturado.="<td>".number_format($total,0, ',', '.')."</td></td>";
			$facturado.="<td>".number_format($totalanulado,0, ',', '.')."</td></tr>";


	
			// potencial
			$total=0;
			$result ="SELECT * FROM sucursales";
			$result=mysql_query($result,$link_caweb) or die('Error sucursales, '.mysql_error());
			while ($row = mysql_fetch_array($result))
			{
				$result2 ="SELECT sucursalfactura,SUM(valoralquiler) AS facturado FROM basefacturacion WHERE estado = 'V' 
						AND sucursalfactura = '$row[codigosucursal]' GROUP BY sucursalfactura";
				$result2=mysql_query($result2,$link_caweb) or die('Error sucursalfactura, '.mysql_error());
				$row2 = mysql_fetch_array($result2);
		
				$potencial.="<td>".number_format($row2[facturado],0, ',', '.')."<td>";
				$total=$total+$row2[facturado];
			}
			
			$potencial.="<td>".number_format($total,0, ',', '.')."</tr>";
	
			echo $sucursales;
			echo $facturadoantes;
			echo $facturadoantes2;
			echo $facturadoantes3;
			echo $facturadoantes4;
			echo $facturadoantes5;
            echo $facturadoantes6;
            echo $facturadoantes7;
            echo $facturadoantes8;
            echo $facturadoantes9;
            echo $facturadoantes10;
            echo $facturadoantes11;
            echo $facturadoantes12;
            echo $facturadoantes13;
            echo $facturadoantes14;
            echo $facturadoantes15;
            echo $facturadoantes16;
            echo $facturadoantes17;
            echo $facturadoantes18;
            echo $facturadoantes19;
			echo $facturado;
			echo $potencial;
	
			echo "</table></center>";
	
		break;
	case "CARCON":
			$condicion="";
			if ($sucursal == 'BOG') $condicion=""; else  $condicion=" WHERE codigosucursal = '$sucursal'";
	
			echo "<center>Resumen de Cartera<br><br>";
			echo "<table border=1>";
			$sucursales="<tr bgcolor='aabbcc'><td>";
			if ($sucursal == 'BOG')
			{
				$rango1="<tr><td><a href=jqgridcartera/index.php?opcion=RES30>De  0 a 30 Dias</a>";
				$rango2="<tr><td><a href=jqgridcartera/index.php?opcion=RES60>De 31 a 60 Dias</a>";
				$rango3="<tr><td><a href=jqgridcartera/index.php?opcion=RES90>De 61 a 90 Dias</a>";
				$rango4="<tr><td><a href=jqgridcartera/index.php?opcion=RES91>Mas de 90 Dias</a>";
				$rango5="<tr><td><a href=jqgridcartera/index.php?opcion=RES92>En jurica</a>";
				$rango6="<tr><td>T O T A L E S : ";
			}
			else
			{
				$rango1="<tr><td>De  0 a 30 Dias";
				$rango2="<tr><td>De 31 a 60 Dias";
				$rango3="<tr><td>De 61 a 90 Dias";
				$rango4="<tr><td>Mas de 90 Dias";
				$rango5="<tr><td>En jurica";
				$rango6="<tr><td>T O T A L E S : ";
			}
			
			$link_caweb = con_caweb();

			$_SESSION['caweb']['carteraparametro00'] = "factura > 0 AND estado <> 'A' AND estadocartera <> 'C'";
							
			// rango1
			$total=0;
			$totaltotal=0;
			$totalbog=0;
			$totalcal=0;
			$totalmed=0;
			$totalbaq=0;
			$result ="SELECT * FROM sucursales $condicion";
			$result=mysql_query($result,$link_caweb) or die('Error sucursales, '.mysql_error());
			while ($row = mysql_fetch_array($result))
			{
				$result2 ="SELECT SUM(cuadre) AS pendiente FROM cabeceras_fac WHERE 
							factura > 0 AND estado <> 'A' AND estadocartera <> 'C' AND juridico <> 'J' AND
							fechafactura BETWEEN CURDATE() - INTERVAL 30 DAY  AND CURDATE() 
							AND sucursal = '$row[codigosucursal]'";

				$_SESSION['caweb']['carteraparametro30'] = "factura > 0 AND estado <> 'A' AND estadocartera <> 'C' AND juridico <> 'J' AND
							fechafactura BETWEEN CURDATE() - INTERVAL 30 DAY  AND CURDATE()";
				
				$result2=mysql_query($result2,$link_caweb) or die('Error cabeceras_fac, '.mysql_error());
				$row2 = mysql_fetch_array($result2);
		
				$rango1.="<td><a href=jqgridcartera/index.php?opcion=".$row[codigosucursal]."30>".number_format($row2[pendiente],0, ',', '.')."</a>";
				$sucursales.="<td><a href=jqgridcartera/index.php?opcion=".$row[codigosucursal]."00>".$row[nombre];
				$total=$total+$row2[pendiente];
				if ($row[codigosucursal]=='BOG') $totalbog = $totalbog + $row2[pendiente];
				if ($row[codigosucursal]=='CAL') $totalcal = $totalcal + $row2[pendiente];
				if ($row[codigosucursal]=='MED') $totalmed = $totalmed + $row2[pendiente];
				if ($row[codigosucursal]=='BAQ') $totalbaq = $totalbaq + $row2[pendiente];
			}
			$totaltotal = $totaltotal + $total;
			$sucursales.="<td> TOTAL </tr>";
			$rango1.="<td>".number_format($total,0, ',', '.')."</tr>";
			

			// rango2
			$total=0;
			$result ="SELECT * FROM sucursales $condicion";
			$result=mysql_query($result,$link_caweb) or die('Error sucursales, '.mysql_error());
			while ($row = mysql_fetch_array($result))
			{
				$result2 ="SELECT SUM(cuadre) AS pendiente FROM cabeceras_fac WHERE 
							factura > 0 AND estado <> 'A' AND estadocartera <> 'C' AND juridico <> 'J' AND
							fechafactura BETWEEN CURDATE() - INTERVAL 60 DAY  AND CURDATE() - INTERVAL 31 DAY  
							AND sucursal = '$row[codigosucursal]'";
							
				$_SESSION['caweb']['carteraparametro60'] = "factura > 0 AND estado <> 'A' AND estadocartera <> 'C' AND juridico <> 'J' AND
							fechafactura BETWEEN CURDATE() - INTERVAL 60 DAY  AND CURDATE() - INTERVAL 31 DAY";
							
				$result2=mysql_query($result2,$link_caweb) or die('Error cabeceras_fac, '.mysql_error());
				$row2 = mysql_fetch_array($result2);

				$rango2.="<td><a href=jqgridcartera/index.php?opcion=".$row[codigosucursal]."60>".number_format($row2[pendiente],0, ',', '.')."</a>";		

				$total=$total+$row2[pendiente];
				if ($row[codigosucursal]=='BOG') $totalbog = $totalbog + $row2[pendiente];
				if ($row[codigosucursal]=='CAL') $totalcal = $totalcal + $row2[pendiente];
				if ($row[codigosucursal]=='MED') $totalmed = $totalmed + $row2[pendiente];
				if ($row[codigosucursal]=='BAQ') $totalbaq = $totalbaq + $row2[pendiente];
			}
			$totaltotal = $totaltotal + $total;
			$rango2.="<td>".number_format($total,0, ',', '.')."</tr>";

			// rango3
			$total=0;
			$result ="SELECT * FROM sucursales $condicion";
			$result=mysql_query($result,$link_caweb) or die('Error sucursales, '.mysql_error());
			while ($row = mysql_fetch_array($result))
			{
				$result2 ="SELECT SUM(cuadre) AS pendiente FROM cabeceras_fac WHERE 
							factura > 0 AND estado <> 'A' AND estadocartera <> 'C' AND juridico <> 'J' AND
							fechafactura BETWEEN CURDATE() - INTERVAL 90 DAY  AND CURDATE() - INTERVAL 61 DAY  
							AND sucursal = '$row[codigosucursal]'";

				$_SESSION['caweb']['carteraparametro90'] = "factura > 0 AND estado <> 'A' AND estadocartera <> 'C' AND juridico <> 'J' AND
							fechafactura BETWEEN CURDATE() - INTERVAL 90 DAY  AND CURDATE() - INTERVAL 61 DAY";
							
				$result2=mysql_query($result2,$link_caweb) or die('Error cabeceras_fac, '.mysql_error());
				$row2 = mysql_fetch_array($result2);
				
				$rango3.="<td><a href=jqgridcartera/index.php?opcion=".$row[codigosucursal]."90>".number_format($row2[pendiente],0, ',', '.')."</a>";
				
				$total=$total+$row2[pendiente];
				if ($row[codigosucursal]=='BOG') $totalbog = $totalbog + $row2[pendiente];
				if ($row[codigosucursal]=='CAL') $totalcal = $totalcal + $row2[pendiente];
				if ($row[codigosucursal]=='MED') $totalmed = $totalmed + $row2[pendiente];
				if ($row[codigosucursal]=='BAQ') $totalbaq = $totalbaq + $row2[pendiente];
			}
			$totaltotal = $totaltotal + $total;
			$rango3.="<td>".number_format($total,0, ',', '.')."</tr>";

			// rango4
			$total=0;
			$result ="SELECT * FROM sucursales $condicion";
			$result=mysql_query($result,$link_caweb) or die('Error sucursales, '.mysql_error());
			while ($row = mysql_fetch_array($result))
			{
				$result2 ="SELECT SUM(cuadre) AS pendiente FROM cabeceras_fac WHERE 
							factura > 0 AND estado <> 'A' AND estadocartera <> 'C' AND juridico <> 'J' AND
							fechafactura < CURDATE() - INTERVAL 90 DAY  
							AND sucursal = '$row[codigosucursal]'";

				$_SESSION['caweb']['carteraparametro91'] = "factura > 0 AND estado <> 'A' AND estadocartera <> 'C' AND juridico <> 'J' AND
							fechafactura < CURDATE() - INTERVAL 90 DAY";

				$result2=mysql_query($result2,$link_caweb) or die('Error cabeceras_fac, '.mysql_error());
				$row2 = mysql_fetch_array($result2);
		
				$rango4.="<td><a href=jqgridcartera/index.php?opcion=".$row[codigosucursal]."91>".number_format($row2[pendiente],0, ',', '.')."</a>";
							
				$total=$total+$row2[pendiente];
				if ($row[codigosucursal]=='BOG') $totalbog = $totalbog + $row2[pendiente];
				if ($row[codigosucursal]=='CAL') $totalcal = $totalcal + $row2[pendiente];
				if ($row[codigosucursal]=='MED') $totalmed = $totalmed + $row2[pendiente];
				if ($row[codigosucursal]=='BAQ') $totalbaq = $totalbaq + $row2[pendiente];
			}
			$totaltotal = $totaltotal + $total;
			$rango4.="<td>".number_format($total,0, ',', '.')."</tr>";






			// rango5 juridicas
			$total=0;
			$result ="SELECT * FROM sucursales $condicion";
			$result=mysql_query($result,$link_caweb) or die('Error sucursales, '.mysql_error());
			while ($row = mysql_fetch_array($result))
			{
				$result2 ="SELECT SUM(cuadre) AS pendiente FROM cabeceras_fac WHERE 
							factura > 0 AND estado <> 'A' AND juridico = 'J'
							AND sucursal = '$row[codigosucursal]'";

				$_SESSION['caweb']['carteraparametro92'] = "factura > 0 AND estado <> 'A' AND estadocartera = 'J'";
				
				$result2=mysql_query($result2,$link_caweb) or die('Error cabeceras_fac, '.mysql_error());
				$row2 = mysql_fetch_array($result2);
		
				$rango5.="<td><a href=jqgridcartera/index.php?opcion=".$row[codigosucursal]."92>".number_format($row2[pendiente],0, ',', '.')."</a>";
				
				$total=$total+$row2[pendiente];
				if ($row[codigosucursal]=='BOG') $totalbog = $totalbog + $row2[pendiente];
				if ($row[codigosucursal]=='CAL') $totalcal = $totalcal + $row2[pendiente];
				if ($row[codigosucursal]=='MED') $totalmed = $totalmed + $row2[pendiente];
				if ($row[codigosucursal]=='BAQ') $totalbaq = $totalbaq + $row2[pendiente];
			}
			$totaltotal = $totaltotal + $total;
			$rango5.="<td>".number_format($total,0, ',', '.')."</tr>";
			
			if ($sucursal == 'BOG') 
			{
				$rango6.="<td>".number_format($totalbog,0, ',', '.');
				$rango6.="<td>".number_format($totalcal,0, ',', '.');
				$rango6.="<td>".number_format($totalmed,0, ',', '.');
				$rango6.="<td>".number_format($totalbaq,0, ',', '.');
				$rango6.="<td>".number_format($totaltotal,0, ',', '.');
			}
			else
			{
			  if($sucursal=='CAL') { $rango6.="<td>".number_format($totalcal,0, ',', '.'); $rango6.="<td>".number_format($totaltotal,0, ',', '.'); }
			  if($sucursal=='MED') { $rango6.="<td>".number_format($totalmed,0, ',', '.'); $rango6.="<td>".number_format($totaltotal,0, ',', '.'); }
			  if($sucursal=='BAQ') { $rango6.="<td>".number_format($totalbaq,0, ',', '.'); $rango6.="<td>".number_format($totaltotal,0, ',', '.'); }
	  		}
			  		



			echo $sucursales;
			echo $rango1;
			echo $rango2;
			echo $rango3;
			echo $rango4;
			echo $rango5;
			echo $rango6;
			
			echo "</table></center>";
	   		break;
  default:								// mensaje por defecto
		echo "<br><br><br><br><br><br><br><br><br>";
		echo "<center><font size=5 color='blue'>Bienvenidos a CA WEB</font></center>";
        break;
}

?>